home *** CD-ROM | disk | FTP | other *** search
/ Megahits 5 / Megahits 5 (1994)(GTI - Rhein-Main-Soft)(DE)(Disc 2 of 2)[!].iso / archive / conv / spectrum2iff_102.lha / Spectrum2IFF / Spectrum2IFF.c < prev    next >
C/C++ Source or Header  |  1994-05-12  |  7KB  |  205 lines

  1. /************************************************************************
  2.         Spectrum2IFF.c
  3.             Converts ZX Spectrum Screens to IFF-ILBM
  4.             It doesn't work from Workbench
  5.         Usage: Spectrum2IFF SpectrumFile ILBMFile
  6.         Version 1.0 Rev2
  7.             14 - V - 1993
  8.         Compilacion:
  9.             Compiled with SAS/Lattice C 5.10a
  10.         Bugs:
  11.             It doesn't work from Workbench
  12.         (c) Eduardo Delgado Buitrago
  13. *************************************************************************/
  14.  
  15.  
  16. #define NAME    "Spectrum2IFF"
  17. #define VERSION "version 1.0"
  18. #define DATE    "14.5.93"
  19.  
  20.  
  21. /* Header files needed -------------------------------------------- */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <libraries/dos.h>
  25. #include <exec/memory.h>
  26. #include <proto/dos.h>
  27. #include <proto/exec.h>
  28.  
  29.  
  30. /* This program's error messages ---------------------------------- */
  31. char *ErrMsg[] = {
  32.     "Unknown error\n" ,
  33.     "Not enough memory\n" ,
  34.     "Can't open file %s for input\n" ,
  35.     "Error reading file %s\n" ,
  36.     "Bad format. %s is not a Spectrum Screen\n" ,
  37.     "Can't create file %s\n" ,
  38.     "Error writing to file %s\n"
  39. } ;
  40. #define MaxErrNo ((sizeof(ErrMsg)/sizeof(char *))-1)
  41.  
  42.  
  43. /* Used in order to remember all resources used ------------------- */
  44. /* So you can (must!) free all of them */
  45. ULONG RememberResources = 0 ;
  46. #define SPECTRUMMEMORY  0x0001
  47.     UBYTE *SpectrumBuffer ;
  48. #define IFFMEMORY   0x0002
  49.     UBYTE *IFFBuffer ;
  50. #define INPUTFILE   0x0004
  51.     BPTR ifh ;
  52. #define OUTPUTFILE  0x0008
  53.     BPTR ofh ;
  54.  
  55.  
  56. /* function prototypes -------------------------------------------- */
  57. void main(int argc , char **argv) ;
  58. void Usage(void) ;
  59. void Error(int ErrNo , char *ErrObj) ;
  60. void FreeResources(void) ;
  61. LONG GetFileSize(BPTR fh) ;
  62. int CXBRK(void) { return(0); }
  63. int chkabort(void) { return(0); }
  64.  
  65.  
  66. /* global variables ----------------------------------------------- */
  67. char *Version = "$VER: "NAME" "VERSION" "DATE"\r\n" ;
  68.  
  69.  
  70. void main(int argc , char *argv[]) /* ----------------------------- */
  71. {
  72.     static UBYTE IFFHeader[] = {
  73.         0x46 , 0x4F , 0x52 , 0x4D , 0x00 , 0x00 , 0x60 , 0x6C ,
  74.         0x49 , 0x4C , 0x42 , 0x4D , 0x42 , 0x4D , 0x48 , 0x44 ,
  75.         0x00 , 0x00 , 0x00 , 0x14 , 0x01 , 0x00 , 0x00 , 0xC0 ,
  76.         0x00 , 0x00 , 0x00 , 0x00 , 0x04 , 0x00 , 0x00 , 0x00 ,
  77.         0x00 , 0x00 , 0x64 , 0x64 , 0x01 , 0x40 , 0x00 , 0xC8 ,
  78.         0x43 , 0x41 , 0x4D , 0x47 , 0x00 , 0x00 , 0x00 , 0x04 ,
  79.         0x00 , 0x00 , 0x60 , 0x00 , 0x43 , 0x4D , 0x41 , 0x50 ,
  80.         0x00 , 0x00 , 0x00 , 0x30 , 0x00 , 0x00 , 0x00 , 0x00 ,
  81.         0x00 , 0xC0 , 0xC0 , 0x00 , 0x00 , 0xC0 , 0x00 , 0xC0 ,
  82.         0x00 , 0xC0 , 0x00 , 0x00 , 0xC0 , 0xC0 , 0xC0 , 0xC0 ,
  83.         0x00 , 0xC0 , 0xC0 , 0xC0 , 0x00 , 0x00 , 0x00 , 0x00 ,
  84.         0x00 , 0xF0 , 0xF0 , 0x00 , 0x00 , 0xF0 , 0x00 , 0xF0 ,
  85.         0x00 , 0xF0 , 0x00 , 0x00 , 0xF0 , 0xF0 , 0xF0 , 0xF0 ,
  86.         0x00 , 0xF0 , 0xF0 , 0xF0 , 0x42 , 0x4F , 0x44 , 0x59 ,
  87.         0x00 , 0x00 , 0x60 , 0x00
  88.     } ;
  89.     static UBYTE ColorCodes[4][16] = {
  90.         { 0,255, 0 ,255, 0 ,255, 0 ,255, 0 ,255, 0 ,255, 0 ,255, 0 ,255 } ,
  91.         { 0, 0 ,255,255, 0 , 0 ,255,255, 0 , 0 ,255,255, 0 , 0 ,255,255 } ,
  92.         { 0, 0 , 0 , 0 ,255,255,255,255, 0 , 0 , 0 , 0 ,255,255,255,255 } ,
  93.         { 0, 0 , 0 , 0 , 0 , 0 , 0 , 0 ,255,255,255,255,255,255,255,255 }
  94.     } ;
  95.     char *InputFile ;
  96.     char *OutputFile ;
  97.     UBYTE *Disp , *Attr , *Target ;
  98.     UBYTE Color , Ink , Paper , Bright , Pixels ;
  99.     int i , j , k , l ;
  100.  
  101.     switch (argc) {
  102.         case 0 : _exit(0L) ;
  103.                  break ;
  104.         case 3 : InputFile = argv[1] ;
  105.                  OutputFile = argv[2] ;
  106.                  break ;
  107.         default: Usage() ;
  108.                  break ;
  109.     }
  110.  
  111.     SpectrumBuffer = AllocMem(6912 , MEMF_PUBLIC) ;
  112.     if (SpectrumBuffer == NULL) Error(1,NULL) ;
  113.     RememberResources |= SPECTRUMMEMORY ;
  114.  
  115.     IFFBuffer = AllocMem(24576 , MEMF_PUBLIC) ;
  116.     if (IFFBuffer == NULL) Error(1,NULL) ;
  117.     RememberResources |= IFFMEMORY ;
  118.  
  119.     ifh = Open(InputFile , MODE_OLDFILE) ;
  120.     if (ifh == NULL) Error(2,InputFile) ;
  121.     RememberResources |= INPUTFILE ;
  122.  
  123.     if (GetFileSize(ifh) != 6912) Error(4,InputFile) ;
  124.     if (Read(ifh , SpectrumBuffer , 6912) != 6912) Error(3,InputFile) ;
  125.     Close(ifh) ;
  126.     RememberResources ^= INPUTFILE ;
  127.  
  128.     Disp = SpectrumBuffer ;
  129.     Attr = SpectrumBuffer + 6144 ;
  130.     Target = IFFBuffer ;
  131.  
  132.     for (i=0 ; i<24 ; i++) {
  133.         for (j=0 ; j<32 ; j++) {
  134.             Color = *Attr++ ;
  135.             Ink = Color & 7 ;
  136.             Paper = (Color >> 3) & 7 ;
  137.             Bright = (Color >> 3) & 8 ;
  138.             Ink += Bright ;
  139.             Paper += Bright ;
  140.             for (k=0 ; k<8 ; k++) {
  141.                 Pixels = Disp[j + 256*k] ;
  142.                 for (l=0 ; l<4 ; l++) {
  143.                     Target[j + 32*l + 128*k] =
  144.                         ((Pixels & ColorCodes[l][Ink]) |
  145.                         (~Pixels & ColorCodes[l][Paper])) ;
  146.                 } /* end for-l */
  147.             } /* end for-k */
  148.         } /* end for-j */
  149.         Disp += ((i%8)==7) ? 1824 : 32 ;
  150.         Target += 1024 ;
  151.     } /* end for-i */
  152.  
  153.     ofh = Open(OutputFile , MODE_NEWFILE) ;
  154.     if (ofh == NULL) Error(5,OutputFile) ;
  155.     RememberResources |= OUTPUTFILE ;
  156.  
  157.     if (Write(ofh , IFFHeader , 116) != 116) Error(6,OutputFile) ;
  158.     if (Write(ofh , IFFBuffer , 24576) != 24576) Error(6,OutputFile) ;
  159.  
  160.     FreeResources() ;
  161.  
  162. } /* end of main() */
  163.  
  164.  
  165. void Usage(void) /* ----------------------------------------------- */
  166. {
  167.     printf("\x1B[1m"NAME" "VERSION"\x1B[0m\n"
  168.            "    Usage: "NAME" SpectrumFile ILBMFile\n"
  169.            "\x1B[1m(c) Eduardo Delgado        " DATE "\x1B[0m\n" ) ;
  170.  
  171.     exit(0) ;
  172. }
  173.  
  174.  
  175. /* On error, free all resources used and exit --------------------- */
  176. void Error(int ErrNo , char *ErrObj)
  177. {
  178.     if (ErrNo > MaxErrNo) printf(ErrMsg[0]) ;
  179.     else if (ErrObj) printf(ErrMsg[ErrNo] , ErrObj) ;
  180.          else printf(ErrMsg[ErrNo]) ;
  181.     FreeResources() ;
  182.     exit(10) ;
  183. }
  184.  
  185.  
  186. /* Free all resources used ---------------------------------------- */
  187. void FreeResources(void)
  188. {
  189.     if (RememberResources & OUTPUTFILE)     Close(ofh) ;
  190.     if (RememberResources & INPUTFILE)      Close(ifh) ;
  191.     if (RememberResources & IFFMEMORY)      FreeMem(IFFBuffer , 24576) ;
  192.     if (RememberResources & SPECTRUMMEMORY) FreeMem(SpectrumBuffer , 6912) ;
  193. }
  194.  
  195.  
  196. LONG GetFileSize(BPTR fh)
  197. {
  198.     register LONG Pos , Size ;
  199.  
  200.     Pos = Seek(fh , 0 , OFFSET_END) ;
  201.     if (Pos < 0) return (-1L) ;
  202.     Size = Seek(fh , Pos , OFFSET_BEGINNING) ;
  203.     return(Size) ;
  204. }
  205.